﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Microsoft.Practices.Unity;
using Vit.Common;
using Vit.Common.Extensions;
using Vit.Web;
using Vit.Web.Helpers;
using Vit.Web.Providers;
using Vit.Web.Models;
using Vit.Site.Models;

namespace Vit.Site.Controllers
{
    public class AccountController : Controller
    {
        [Vit.Web.Mvc.Attributes.Widget]
        public ActionResult LogIn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogIn(LogOnModel model, string returnUrl)
        {
            ICaptchaGenerator captcha = Unity.Current.Resolve<ICaptchaGenerator>();
            bool verified = captcha.Verify(this.Request.Form["VerifyCode"]);
            if(!verified)
                ModelState.AddModelError("", "The Verify Code is incorrect.");
            if (verified && ModelState.IsValid)
            {
                if (Unity.Current.ResolveAny<IMembershipProvider>().ForEach<IMembershipProvider, bool>(o => o.Login(model.UserName, model.Password, false)))
                {
                    if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
                    {
                        return RedirectPermanent(returnUrl);
                    }
                    else
                    {
                        string url = PortalContext.Current.User.Profiles.GetProfileValue("LogonUrl", "/Home/Index");
                        return RedirectPermanent(url);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            }

            return View(model);
        }

        public ActionResult LogOff()
        {
            Unity.Current.ResolveAny<IMembershipProvider>().ForEach<IMembershipProvider>(o => o.Logout());
            return Redirect(FormsAuthentication.LoginUrl);
        }

        [Vit.Web.Mvc.Attributes.Widget]
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                string error = string.Empty;
                var user = Unity.Current.ResolveAny<IMembershipProvider>().ForEach<IMembershipProvider, User>(o => o.CreateUser(model.UserName, model.UserName, model.Password, model.Email, null, null, true, out error));

                if (user != null)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", error);
                }
            }

            return View(model);
        }

        [Authorize]
        [Vit.Web.Mvc.Attributes.Widget]
        public ActionResult ChangePassword()
        {
            return View();
        }

        [Authorize]
        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                if (Unity.Current.ResolveAny<IMembershipProvider>().ForEach<IMembershipProvider, bool>(o => o.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword)))
                {
                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
                }
            }

            return View(model);
        }

        public ActionResult ChangePasswordSuccess()
        {
            return View();
        }
    }
}
